import pytest
import allure
from playwright.sync_api import Page, expect

@allure.feature("用户管理")
@allure.story("用户管理功能")
class TestUserManagement:

    @allure.title("测试用户管理页面基本元素")
    @allure.severity(allure.severity_level.NORMAL)
    def test_user_management_page_elements(self, page: Page, login):
        """测试用户管理页面的基本元素"""
        with allure.step("导航到用户管理页面"):
            page.goto("http://localhost:8080/user/management")
        
        with allure.step("检查页面标题"):
            expect(page.get_by_text("用户管理")).to_be_visible()
        
        with allure.step("检查搜索区域"):
            expect(page.get_by_placeholder("请输入用户名")).to_be_visible()
            expect(page.get_by_role("button", name="搜索")).to_be_visible()
            expect(page.get_by_role("button", name="重置")).to_be_visible()
        
        with allure.step("检查操作按钮"):
            expect(page.get_by_role("button", name="新增用户")).to_be_visible()
        
        with allure.step("检查表格列头"):
            table_headers = ["用户名", "邮箱", "手机号", "角色", "状态", "创建时间", "操作"]
            for header in table_headers:
                expect(page.get_by_text(header)).to_be_visible()

    @allure.title("测试搜索用户功能")
    @allure.severity(allure.severity_level.NORMAL)
    def test_search_user(self, page: Page, login):
        """测试搜索用户功能"""
        with allure.step("导航到用户管理页面"):
            page.goto("http://localhost:8080/user/management")
        
        with allure.step("输入搜索关键词"):
            page.get_by_placeholder("请输入用户名").fill("testuser")
            page.get_by_role("button", name="搜索").click()
        
        with allure.step("验证搜索结果"):
            expect(page.get_by_text("testuser")).to_be_visible()
        
        with allure.step("测试重置功能"):
            page.get_by_role("button", name="重置").click()
            expect(page.get_by_placeholder("请输入用户名")).to_have_value("")

    @allure.title("测试添加新用户功能")
    @allure.severity(allure.severity_level.CRITICAL)
    def test_add_new_user(self, page: Page, login):
        """测试添加新用户功能"""
        with allure.step("导航到用户管理页面"):
            page.goto("http://localhost:8080/user/management")
        
        with allure.step("点击新增用户按钮"):
            page.get_by_role("button", name="新增用户").click()
        
        with allure.step("填写用户信息"):
            page.get_by_placeholder("请输入用户名").fill("newuser123")
            page.get_by_placeholder("请输入密码").fill("Password123!")
            page.get_by_placeholder("请输入邮箱").fill("newuser123@example.com")
            page.get_by_placeholder("请输入手机号").fill("13900139000")
        
        with allure.step("选择角色"):
            page.get_by_text("请选择角色").click()
            page.get_by_text("普通用户").click()
        
        with allure.step("保存"):
            page.get_by_role("button", name="确定").click()
        
        with allure.step("验证成功提示"):
            expect(page.locator('.el-message--success')).to_be_visible()
        
        with allure.step("验证新用户显示在列表中"):
            expect(page.get_by_text("newuser123")).to_be_visible()

    @allure.title("测试编辑用户功能")
    @allure.severity(allure.severity_level.NORMAL)
    def test_edit_user(self, page: Page, login):
        """测试编辑用户功能"""
        with allure.step("导航到用户管理页面"):
            page.goto("http://localhost:8080/user/management")
        
        with allure.step("点击编辑按钮"):
            page.get_by_role("button", name="编辑").first.click()
        
        with allure.step("修改邮箱"):
            page.get_by_placeholder("请输入邮箱").fill("updated@example.com")
        
        with allure.step("保存修改"):
            page.get_by_role("button", name="确定").click()
        
        with allure.step("验证成功提示"):
            expect(page.locator('.el-message--success')).to_be_visible()
        
        with allure.step("验证更新后的邮箱显示在列表中"):
            expect(page.get_by_text("updated@example.com")).to_be_visible()

    @allure.title("测试删除用户功能")
    @allure.severity(allure.severity_level.CRITICAL)
    def test_delete_user(self, page: Page, login):
        """测试删除用户功能"""
        with allure.step("导航到用户管理页面"):
            page.goto("http://localhost:8080/user/management")
        
        with allure.step("获取当前用户总数"):
            initial_count = len(page.locator("tbody tr").all())
            allure.attach(str(initial_count), name="初始用户数", attachment_type=allure.attachment_type.TEXT)
        
        with allure.step("点击删除按钮"):
            page.get_by_role("button", name="删除").first.click()
        
        with allure.step("确认删除"):
            page.get_by_role("button", name="确定").click()
        
        with allure.step("验证成功提示"):
            expect(page.locator('.el-message--success')).to_be_visible()
        
        with allure.step("验证用户总数减少"):
            final_count = len(page.locator("tbody tr").all())
            allure.attach(str(final_count), name="删除后用户数", attachment_type=allure.attachment_type.TEXT)
            expect(final_count).to_be_less_than(initial_count)

    @allure.title("测试修改用户状态功能")
    @allure.severity(allure.severity_level.NORMAL)
    def test_change_user_status(self, page: Page, login):
        """测试修改用户状态功能"""
        with allure.step("导航到用户管理页面"):
            page.goto("http://localhost:8080/user/management")
        
        with allure.step("点击状态开关"):
            status_switch = page.locator('.el-switch').first
            initial_status = status_switch.is_checked()
            allure.attach(str(initial_status), name="初始状态", attachment_type=allure.attachment_type.TEXT)
            status_switch.click()
        
        with allure.step("确认修改"):
            page.get_by_role("button", name="确定").click()
        
        with allure.step("验证成功提示"):
            expect(page.locator('.el-message--success')).to_be_visible()
        
        with allure.step("验证状态已改变"):
            final_status = status_switch.is_checked()
            allure.attach(str(final_status), name="修改后状态", attachment_type=allure.attachment_type.TEXT)
            expect(final_status).not_to_equal(initial_status)

    @allure.title("测试用户角色筛选功能")
    @allure.severity(allure.severity_level.MINOR)
    def test_user_role_filter(self, page: Page, login):
        """测试用户角色筛选功能"""
        with allure.step("导航到用户管理页面"):
            page.goto("http://localhost:8080/user/management")
        
        with allure.step("点击角色筛选"):
            page.get_by_text("请选择角色").click()
            page.get_by_text("管理员").click()
        
        with allure.step("验证筛选结果"):
            roles = page.locator("tbody td:nth-child(4)").all()  # 假设角色是第4列
            for role in roles:
                expect(role).to_have_text("管理员")
        
        with allure.step("清除筛选"):
            page.get_by_text("请选择角色").click()
            page.keyboard.press("Escape")

    @allure.title("测试分页功能")
    @allure.severity(allure.severity_level.MINOR)
    def test_pagination(self, page: Page, login):
        """测试分页功能"""
        with allure.step("导航到用户管理页面"):
            page.goto("http://localhost:8080/user/management")
        
        with allure.step("检查分页控件是否存在"):
            expect(page.locator('.el-pagination')).to_be_visible()
        
        with allure.step("切换每页显示数量"):
            page.locator('.el-pagination__sizes').click()
            page.get_by_text("20 条/页").click()
        
        with allure.step("验证表格行数变化"):
            rows_count = len(page.locator("tbody tr").all())
            allure.attach(str(rows_count), name="表格行数", attachment_type=allure.attachment_type.TEXT)
            expect(rows_count).to_be_less_than_or_equal(20)
        
        with allure.step("测试页面切换"):
            if page.get_by_role("button", name="下一页").is_enabled():
                page.get_by_role("button", name="下一页").click()
                expect(page.locator('.el-pagination__pager .active')).to_have_text("2") 